package MetroLab.Path;
import MetroLab.Utils.ObjectFactory;
import javax.media.j3d.*;
import javax.vecmath.*;
/**
 *
 * @author plus
 */
public abstract class Rail extends Group implements Followable {
    private String Desc;    //描述
    private double Length;  //长度
    //public abstract PosPoint makePath(PosPoint pos) ;

    public double getLength() {
        return Length;
    }

    public void setLength(double Length) {
        this.Length = Length;
    }
    public Rail() {
        Desc="Default Railway";
        Length=0;
        
        makeRail();
    }

    public Rail(String Desc, double Length) {
        this.Desc = Desc;
        this.Length = Length;
        
        makeRail();
    }
    
    private void makeRail(){
        LineArray CenterLine = new LineArray((int) Length * 6+2, LineArray.COORDINATES | LineArray.COLOR_3);
               // OutterLine = new LineArray((int) Length * 6+2, LineArray.COORDINATES | LineArray.COLOR_3),
               // InnerLine = new LineArray((int) Length * 6+2, LineArray.COORDINATES | LineArray.COLOR_3);
        PosPoint pos = new PosPoint(0);
        Point3d tempRailPoints[];
        RailModelFactory RailFactory=new RailModelFactory(this);
        pos = makePath(pos);
        
        for (int i = 0; i <= (int) Length * 3; i++) {
            
            tempRailPoints=getRailPoints(pos);
            /*OutterLine.setCoordinate(2*i, tempRailPoints[0]);
            InnerLine.setCoordinate(2*i, tempRailPoints[1]);
            OutterLine.setColor(2*i, new Color3f(0f, 1f, 0f));
            InnerLine.setColor(2*i, new Color3f(0f, 0f, 1f));*/
            CenterLine.setCoordinate(2*i, tempRailPoints[2]);
            CenterLine.setColor(2*i, new Color3f(0.5f, 0.5f, 0.5f));
            
            pos.d = (double) ((i+1d) / 3d);
            pos = makePath(pos);
            
            tempRailPoints=getRailPoints(pos);
           /* OutterLine.setCoordinate(2*i+1, tempRailPoints[0]);
            InnerLine.setCoordinate(2*i+1, tempRailPoints[1]);
            OutterLine.setColor(2*i+1, new Color3f(0f, 1f, 0f));
            InnerLine.setColor(2*i+1, new Color3f(0f, 0f, 1f));*/
            CenterLine.setCoordinate(2*i+1, tempRailPoints[2]);
            CenterLine.setColor(2*i+1, new Color3f(0.5f, 0.5f, 0.5f));
        }
        LineAttributes LineAtt = new LineAttributes(2, LineAttributes.PATTERN_SOLID, true);
        Appearance LineApp = new Appearance();
        LineApp.setLineAttributes(LineAtt);
        
        this.addChild(new Shape3D(CenterLine,LineApp));
        //this.addChild(new Shape3D(InnerLine,LineApp));
        //this.addChild(new Shape3D(OutterLine,LineApp));
        
        //addChild(getDioms());
        //addChild(ObjectFactory.createObject("Model/Rail/10MLift",true));
        addChild(RailFactory.MakeRail(0, this.Length));
        addChild(RailFactory.MakeBase(0, this.Length));
    }
    
    private Point3d[] getRailPoints(PosPoint p){
        Point3d res[]={new Point3d(0,0,-0.7175),new Point3d(0,0,0.7175),new Point3d(0,5.4,0)};
        Transform3D transH=new Transform3D();
        transH.rotX(p.AngH);
        Transform3D transV=new Transform3D();
        transV.rotY(p.AngV);
        Transform3D transT=new Transform3D();
        transT.set(new Vector3d(p.pos.x,p.pos.y,p.pos.z));
        
        transT.mul(transV);
        transT.mul(transH);
        
        /*transT.transform(res[0]);
        transT.transform(res[1]);*/
        transT.transform(res[2]);
        return res;
    }
    
    public Group getDioms(){
		LineArray [] Dioms={new LineArray(2,LineArray.COORDINATES|LineArray.COLOR_3),
							new LineArray(2,LineArray.COORDINATES|LineArray.COLOR_3),
							new LineArray(2,LineArray.COORDINATES|LineArray.COLOR_3)};
		Dioms[0].setCoordinate(0, new Point3f(-1000,0,0));
		Dioms[0].setCoordinate(1, new Point3f(1000,0,0));
			Dioms[0].setColors(0, new Color3f[]
			                   {new Color3f(1f,0f,0f),new Color3f(1f,0f,0f)});
		
		Dioms[1].setCoordinate(0, new Point3f(0,-1000,0));
		Dioms[1].setCoordinate(1, new Point3f(0,1000,0));
			Dioms[1].setColors(0, new Color3f[]
		   			           {new Color3f(0f,1f,0f),new Color3f(0f,1f,0f)});
		
		Dioms[2].setCoordinate(0, new Point3f(0,0,-1000));
		Dioms[2].setCoordinate(1, new Point3f(0,0,1000));
			Dioms[2].setColors(0, new Color3f[]
		   			            {new Color3f(0f,0f,1f),new Color3f(0f,0f,1f)});
		Group DiomGroup=new Group();
		LineAttributes LineAtt=new LineAttributes(1,LineAttributes.PATTERN_SOLID,true);
		Appearance LineApp=new Appearance();
		LineApp.setLineAttributes(LineAtt);
		for (int i=0; i<3;i++){
			DiomGroup.addChild(new Shape3D(Dioms[i],LineApp));
		}
		return DiomGroup;
		
	}
    
}
